﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SBBitmap;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace cellAnalysis
{
    public class TImage
    {
        public CustomControls.HB_Template_Control control;
        private WarpBitmap FastImg;
        private String m_ImgPath;

        #region Konstruktoren
        public TImage(int width, int height, System.Drawing.Imaging.PixelFormat pixelformat)
        {
            control = new CustomControls.HB_Template_Control();
            FastImg = new WarpBitmap(width, height, pixelformat);
        }
        public TImage(Bitmap bmp)
        {
            control = new CustomControls.HB_Template_Control();
            FastImg = new WarpBitmap(bmp);
        }
        public TImage(String ImageFileName)
        {
            control = new CustomControls.HB_Template_Control();
            FastImg = new WarpBitmap(ImageFileName);
            m_ImgPath = ImageFileName;
            ImagePath = ImageFileName;
        }
        #endregion

        #region Methoden
        public byte GetPixel(int x, int y)
        {
            return FastImg.GetPixelB(x, y);
        }
        public Color GetRGBPixel(int x, int y)
        {
            return FastImg.GetPixel(x, y);
        }
        public void SetPixel(int x, int y, byte GrayVal)
        {
            FastImg.SetPixel(x, y, GrayVal);
        }
        public void SetRGBPixel(int x, int y, Color color)
        {
            FastImg.SetPixel(x, y, color);
        }
        public TImage GetCopy()
        {
            Color color;

            TImage img = new TImage(this.Width, this.Height, this.Pixelformat);
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    color = this.FastImg.GetPixel(j, i);
                    img.SetRGBPixel(j, i, Color.FromArgb(color.R, color.G, color.B));
                }
            }
            return img;
        }
        #endregion

        #region Properties
        public Bitmap Image
        {
            set
            {
                FastImg = new WarpBitmap(value);
            }
            get
            {
                return FastImg.Image;
            }
        }
        public int Width
        {
            get
            {
                return FastImg.Width;
            }
        }
        public int Height
        {
            get
            {
                return FastImg.Height;
            }
        }
        public bool Locked
        {
            set
            {
                try
                {
                    FastImg.Locked = value;
                }
                catch (System.Exception ex)
                {
                    System.Exception baseEx = ex.GetBaseException();
                    //MessageBox.Show(ex.GetBaseException().Message);
                }
            }
        }
        public PixelFormat Pixelformat
        {
            get
            {
                return FastImg.Pixelformat;
            }
        }
        public IntPtr firstPixel
        {
            get
            {
                return FastImg.m_firstline;
            }
        }
        public String ImagePath
        {
            set
            {
                m_ImgPath = value;
            }
            get
            {
                return m_ImgPath;
            }
        }
        #endregion
    }
}
